﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.OpenProject
{
    public partial class openprojectlist : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("KFSYGL");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");
            if (!Page.IsPostBack)
            {
                bind("");
                btDel.Attributes.Add("onclick", "javascript:return confirm('你确认要删除选定项目吗?')");
                bindddl();
            }
        }

        void bindddl()
        {
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            mLab = blab.GetModelList(" Lab_ParentID is null ");
            ddlLab.DataSource = mLab;
            ddlLab.DataTextField = "Lab_Name";
            ddlLab.DataValueField = "Lab_Code";
            ddlLab.DataBind();
            ddlLab.Items.Insert(0, new ListItem("", "0"));

            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetItemListByDClass("ruxuenianfen");
            ddlYear.DataSource = listCClass;
            ddlYear.DataTextField = "Name";
            ddlYear.DataValueField = "Code";
            ddlYear.DataBind();
            ddlYear.Items.Insert(0, new ListItem("", "0"));
        }

        void bind(string strwhere)
        {
            LabMS.Model.SysSetting msys = new LabMS.Model.SysSetting();
            ExtendBLL.SysSetting extendsys = new ExtendBLL.SysSetting();
            msys = extendsys.GetCurrentSetting();

            List<LabMS.Model.OpentProject> listmopproject = new List<LabMS.Model.OpentProject>();
            LabMS.BLL.OpentProject bopproject = new LabMS.BLL.OpentProject();


            if (strwhere.Equals(""))
            {
                strwhere += " 1=1 ";
            }

            if (UserType == "teacher")
            {
                strwhere += " and  OP_Teacher = '" + UserCode + "'";
            }

            listmopproject = bopproject.GetModelList(strwhere + " order by ID desc ");

            foreach (LabMS.Model.OpentProject op in listmopproject)
            {
                List<LabMS.Model.Teacher> listmteacher = new List<LabMS.Model.Teacher>();
                LabMS.BLL.Teacher bteacher = new LabMS.BLL.Teacher();
                listmteacher = bteacher.GetModelList(" Teacher_Code= '" + op.OP_Teacher + "'");
                if (listmteacher.Count > 0)
                {
                    op.OP_Teacher = listmteacher[0].Teacher_Name;
                }

                LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
                List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
                mLab = blab.GetModelList(" Lab_Code='" + op.Project_Lab + "'");
                if (mLab.Count > 0)
                {
                    op.Project_Lab = mLab[0].Lab_Name;
                }
                ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
                List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
                listCClass = eDictionary.GetModelList("DClass = 'shiyanyaoqiu' and Code = '" + op.Project_Require + "'");
                if (listCClass.Count > 0)
                {
                    op.Project_Require = listCClass[0].Name;
                }
                op.OP_Phone = op.Project_StartWeek.ToString() + "--" + op.Project_EndWeek.ToString();
                List<LabMS.Model.ProjectAppointment> listprojectapp = new List<LabMS.Model.ProjectAppointment>();
                ExtendBLL.ProjectAppointment bprojectapp = new ExtendBLL.ProjectAppointment();
                listprojectapp = bprojectapp.GetModelList(" OP_ProjectCode ='" + op.OP_ProjectCode + "'");
                op.OP_OpenType = listprojectapp.Count.ToString();
                listCClass = eDictionary.GetModelList("DClass = 'shiyanleixing' and Code = '" + op.Project_Type + "'");
                if (listCClass.Count > 0)
                {
                    op.Project_Type = listCClass[0].Name;
                }
                if (op.OP_Audit == "0")
                {

                }
                op.OP_Audit = LabMS.Common.Status.GetOpenProjectStatus(op.OP_Audit);
                if (op.Semester == 1)
                {
                    op.Project_Subject = "春季学期";
                }
                else if (op.Semester == 2)
                {
                    op.Project_Subject = "秋季学期";
                }
            }

            gv_OpenProjectList.DataSource = listmopproject;
            gv_OpenProjectList.DataBind();
        }

        protected void btDel_Click(object sender, EventArgs e)
        {
            lbErr.Visible = false;
            CheckBox ch;
            LabMS.BLL.OpentProject bopentproject = new LabMS.BLL.OpentProject();
            ExtendBLL.ProjectAppointment extendprojectapp = new ExtendBLL.ProjectAppointment();
            try
            {
                for (int i = 0; i < gv_OpenProjectList.Rows.Count; i++)
                {
                    ch = (CheckBox)gv_OpenProjectList.Rows[i].FindControl("checkdel");
                    if (ch.Checked)
                    {
                        extendprojectapp.Del(" where OP_ProjectCode='" + gv_OpenProjectList.DataKeys[i]["OP_ProjectCode"].ToString() + "'");
                        string CID = gv_OpenProjectList.DataKeys[i]["ID"].ToString();
                        bopentproject.Delete(int.Parse(CID));
                    }
                }
                //checkall.Checked = false;
                if (lbIsSearch.Text == "0")
                {
                    bind("");
                }
                else
                {
                    string strWhere = " 1=1 ";
                    if (ddlLab.SelectedValue != "0")
                    {
                        strWhere += "and Project_Lab = '" + ddlLab.SelectedValue + "'";
                    }
                    if (ddlStatus.SelectedValue != "-1")
                    {
                        strWhere += "and OP_Audit = '" + ddlStatus.SelectedValue + "'";
                    }
                    if (ddlYear.SelectedValue != "0")
                    {
                        strWhere += "and SYear = '" + ddlYear.SelectedValue + "'";
                    }
                    if (ddlSemester.SelectedValue != "-1")
                    {
                        strWhere += "and Semester = '" + ddlSemester.SelectedValue + "'";
                    }
                    if (!tbProjectName.Text.Trim().Equals(""))
                    {
                        strWhere += "and OP_ProjectName like '%" + tbProjectName.Text.Trim() + "%'";
                    }
                    bind(strWhere);
                }

                Common.JShelper.JSAlert(Page, "success", "删除成功!");
            }
            catch (Exception ex)
            {
                Common.JShelper.JSAlert(Page, "err", "删除失败!");
                lbErr.Visible = true;
                lbErr.Text = ex.Message;
            }
        }

        protected void GridViewConsum_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv_OpenProjectList.PageIndex = e.NewPageIndex;
            if (lbIsSearch.Text == "0")
            {
                bind("");
            }
            else
            {
                string strWhere = " 1=1 ";
                if (ddlLab.SelectedValue != "0")
                {
                    strWhere += "and Project_Lab = '" + ddlLab.SelectedValue + "'";
                }
                if (ddlStatus.SelectedValue != "-1")
                {
                    strWhere += "and OP_Audit = '" + ddlStatus.SelectedValue + "'";
                }
                if (ddlYear.SelectedValue != "0")
                {
                    strWhere += "and SYear = '" + ddlYear.SelectedValue + "'";
                }
                if (ddlSemester.SelectedValue != "-1")
                {
                    strWhere += "and Semester = '" + ddlSemester.SelectedValue + "'";
                }
                if (!tbProjectName.Text.Trim().Equals(""))
                {
                    strWhere += "and OP_ProjectName like '%" + tbProjectName.Text.Trim() + "%'";
                }
                bind(strWhere);
            }
        }

        protected void GridViewConsum_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["style"] = "Cursor:pointer";
                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)
                {
                    e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='openprojectperview.aspx?ID="
                                        + gv_OpenProjectList.DataKeys[gv_OpenProjectList.DataKeys.Count - 1]["ID"].ToString() + "&where=0'");
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strWhere = " 1=1 ";
            if (ddlLab.SelectedValue != "0")
            {
                strWhere += "and Project_Lab = '" + ddlLab.SelectedValue + "'";
            }
            if (ddlStatus.SelectedValue != "-1")
            {
                strWhere += "and OP_Audit = '" + ddlStatus.SelectedValue + "'";
            }
            if (ddlYear.SelectedValue != "0")
            {
                strWhere += "and SYear = " + ddlYear.SelectedValue;
            }
            if (ddlSemester.SelectedValue != "-1")
            {
                strWhere += "and Semester = " + ddlSemester.SelectedValue;
            }
            if (!tbProjectName.Text.Trim().Equals(""))
            {
                strWhere += "and OP_ProjectName like '%" + tbProjectName.Text.Trim() + "%'";
            }
            bind(strWhere);
            lbIsSearch.Text = "1";
        }

        protected void btnRest_Click(object sender, EventArgs e)
        {
            ddlStatus.SelectedIndex = 0;
            ddlLab.SelectedIndex = 0;
            tbProjectName.Text = "";
            bind("");
            lbIsSearch.Text = "0";
        }
    }
}
